package servlet;

import java.io.IOException;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import factory.DAOFactory;
import vo.Advertise;
import vo.Person;

/**
 * Servlet implementation class AddAdServlet
 */
@WebServlet("/AddAdServlet")
public class AddAdServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddAdServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HttpSession session = request.getSession(true);
		Person person = (Person) session.getAttribute("user");
		int id = person.getUser_id();
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String time = "";
		Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        time = sdf.format(d);  
        Advertise advertise = new Advertise();
        advertise.setTitle(title);
        advertise.setContent(content);
        advertise.setTime(time);
        advertise.setSender(id);
        int result=0;
        result = DAOFactory.getAdDAOInstance().Add(advertise);
        if(result>=0) {
        	response.sendRedirect("store_ad.jsp");
        }else {
        	response.sendRedirect("store_ad_create.jsp");
        }
		
	}

}
